package jsyf.oa.web.doc;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import jsyf.com.util.BaseUtils;
import jsyf.com.util.FastJsonConvert;
import jsyf.com.util.ResponseUtils;
import jsyf.oa.facade.doc.TypeFacade;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by Administrator on 2018/4/20.
 */
@Api(value = "文档Type接口", description = "文档Type接口")
@Controller
@RequestMapping("/docType")
public class TypeController {

    @Resource
    private TypeFacade typeFacade;

    @RequestMapping(value = "/save.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "添加类型",
            notes = "添加类型",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData",value = "{name:'office2017',url:'',delFlag:'0',sortNo:'2'}",dataType = "String", paramType = "body")
    public void save(HttpServletResponse response, @RequestBody String jsonData) {
        String resultId = this.typeFacade.save(jsonData);
        String result = "";
        if ("".equals(resultId)) {
            result = BaseUtils.errorrRespond("服务器错误");
        } else {
            JSONObject obj = new JSONObject();
            obj.put("id", resultId);
            result = BaseUtils.succsessObjRespond(obj);
        }
        ResponseUtils.putJsonResponse(response, result);
    }

    @RequestMapping(value = "/delById.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "删除类型",
            notes = "删除类型",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData",value = "{id:'2018042713184E2CA32EB5C8B12A6697'}",dataType = "String", paramType = "body")
    public void delById(HttpServletResponse response, @RequestBody String jsonData) {
        Boolean records = this.typeFacade.delById(jsonData);
        JSONObject obj = new JSONObject();
        obj.put("isOk", records);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(obj));
    }

    @RequestMapping(value = "/getTypeList.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "获取类型",
            notes = "获取类型",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    public void getTypeList(HttpServletResponse response,@RequestBody String jsonData) {
        List<JSONObject> result = this.typeFacade.getTypeList();
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrRespond(result));
    }

    @RequestMapping(value = "/getTypeListByPage.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "分页获取类型",
            notes = "分页获取类型",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    public void getTypeListByPage(HttpServletResponse response,@RequestBody String jsonData) {
        JSONObject result = this.typeFacade.getTypeListByPage(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjHourRespond(result));
    }

    @RequestMapping(value = "/getTypeById.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "根据id获取类型",
            notes = "根据id获取类型",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData",value = "{id:'2018042321034BD5BE13710F5A2FD3B5'}",dataType = "String", paramType = "body")
    public void getTypeById(HttpServletResponse response,@RequestBody String jsonData) {
        JSONObject result = this.typeFacade.getTypeById(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjHourRespond(result));
    }


}